package com.kzw.misc.utils;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
 * Echarts数据转换工具类
 * */
public class EChartUtil {

	/**
	 * 根据标签构造data
	 */
	public static List<Map<String, Object>> init(List<String> labels) {
		List<Map<String, Object>> data = new ArrayList<>();
		for (String name : labels) {
			Map<String, Object> map = new LinkedHashMap<>();
			map.put("name", name);
			map.put("value", 0);
			data.add(map);
		}

		return data;
	}

	/**
	 * 设置值
	 */
	public static void assign(List<Map<String, Object>> data, String name, Object val) {
		for (Map<String, Object> map : data) {
			if (map.get("name").equals(name)) {
				double v1 = Double.parseDouble(map.get("value") + "");
				double v2 = Double.parseDouble(val + "");
				map.put("value", v1 + v2);
				break;
			}
		}
	}

	/**
	 * 转换成echarts需要的标签和数据
	 * names为输出参数
	 */
	public static List<Map<String, Object>> transfer(Map<String, Object> map, List<String> names) {
		List<Map<String, Object>> data = new ArrayList<>();
		names.clear();

		Set<Entry<String, Object>> entrySet = map.entrySet();
		for (Entry<String, Object> entry : entrySet) {
			names.add(entry.getKey());
			Map<String, Object> sub = new LinkedHashMap<>();
			sub.put("name", entry.getKey());
			sub.put("value", entry.getValue());
			data.add(sub);
		}

		return data;
	}
}
